# Patch Rails "ActionController::Resources::Resource" class, 
module AutoRest
  
  # A simple container for storing the ActionController::Resources::Resource
  # objects that the routes manager creates (because rails does not store
  # those objects anywhere)
  class RailsResources
    @@resources_array= []
    @@resources_parents= {}
    
    class << self
      def add resource
        @@resources_array << resource
      end
      
      def all
        @@resources_array
      end
      
      def clear
        @@resources_array.clear      
        @@resources_parents.clear
      end
      
      def of_controller controller
        @@resources_array.select do |res|
          res.controller == controller
        end
      end
      
      # return a hash whose keys are the resources' controller names and values 
      # for each key are  the names of the controllers for that resource parents.
      def resources_parents
        if @@resources_parents.length > 0
          @@resources_parents
        else
          analyze()
        end
      end
    
    private
      
      # Update resources with parent/child relationship info.
      # Looks for relationsips by combining every object in groups of two.
      # How? When nested resources are created, :path_prefix of the child is equal
      # to :nesting_path_prefix of the parent.
      # See actionpack resource.rb's map_resource or map_singleton_resource, 
      # (here I "transcribe" the usefull part):
      #
        #      def map_resource(entities, options = {}, &block)
        #        resource = Resource.new(entities, options)
        #
        #        with_options :controller => resource.controller do |map|
        #          map_collection_actions(map, resource)
        #          map_default_collection_actions(map, resource)
        #          map_new_actions(map, resource)
        #          map_member_actions(map, resource)
        #
        #          if block_given?
        # here!>>>   with_options(:path_prefix => resource.nesting_path_prefix, &block)
        #          end
        #        end
        #      end      
        #
      def analyze
        @@resources_parents.clear
        # Look for relationships on every permutation of resources (in groups of 2).
        @@resources_array.perm(2) do |res1, res2|
          if res1.path_prefix == res2.nesting_path_prefix 
            (@@resources_parents[res1.controller]||= Set.new) << res2.controller
          end
        end
        @@resources_parents
      end
    end
  end
end

# I Patch the Resource class of Rails to store the objects for later usage
# Rails dosn't seem to store these objects anywhere after usage, wich is bad
# because they contain plenty of usefull information (that auto_rest needs!)
# Objects will be stored on AutoRest::RailsResource collection.
class ActionController::Resources::Resource
  alias_method :initialize_before_auto_rest, :initialize

  def initialize *args
    initialize_before_auto_rest(*args)
    AutoRest::RailsResources.add(self)
  end
end
